﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.Account;
using Bricks.Site.Models;
using Bricks.Business.Service;
using Bricks.Business.Data.Repositories.SafetyManagement;
using Bricks.Business.Models.SafetyManagement;


namespace ChiDao.Business.Service.Impl
{
    /// <summary>
    /// 安全管理-环保通报
    /// </summary>
    [Export(typeof(IEnvironmentalService))]
    internal class EnvironmentalService : IEnvironmentalService
    {
        [Import]
        protected IEnvironmentalRepository EnvironmentalRepository { get; set; }

        public IQueryable<Environmental> Environmentals
        {
            get { return EnvironmentalRepository.Entities; }
        }

        public OperationResult Add(Environmental model)
        {
            Environmental record = model.CopyTo<Environmental>();
            //record.ID = GuidHelper.NewGuid();
            EnvironmentalRepository.Insert(record);
            return new OperationResult(OperationResultType.Success, "新增成功", record);
        }

        public OperationResult Modify(Environmental model)
        {
            PublicHelper.CheckArgument(model, "model");
            Environmental record = Environmentals.FirstOrDefault(m => m.ID == model.ID);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            record = model.CopyTo<Environmental>();
            if (EnvironmentalRepository.Update(record) >= 0)
                return new OperationResult(OperationResultType.Success, "修改成功", record);
            return new OperationResult(OperationResultType.Error, "保存失败");
        }

        public OperationResult Delete(Guid id)
        {
            Environmental record = Environmentals.FirstOrDefault(m => m.ID == id);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            if (EnvironmentalRepository.Delete(record) >= 0)
                return new OperationResult(OperationResultType.Success, "删除成功", record);
            return new OperationResult(OperationResultType.Error, "删除失败");
        }

        /// <summary>
        /// 根据通报ID获取通报名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetCircularNameByID(Guid id)
        {
            string result = string.Empty;

            var record = Environmentals.FirstOrDefault(m => m.ID == id);
            if (record != null)
            {
                result = record.CircularName;
            }
            return result;
        }
    }
}